﻿using CommonContainer.Mockery.Unity12;
using Mockery.Core.CommonContainer;
using NUnit.Framework;

namespace CommonContainer.UnitTest.Unity12
{
    [TestFixture]
    public class UnityContainerBuilderTests
    {
        private const int DefaultTestValue = 0;

        [Test]
        public void CommonContainer_InvariantTests()
        {
            TestUtility.AssertMefExport<UnityContainerBuilder, ICommonContainerBuilder>();
        }


        [Test]
        public void UnityContainerBuilder_NewTest()
        {
            var builder = new UnityContainerBuilder();

            Assert.That(builder != builder.New());

        }

        [Test]
        public void CommonContainerBuilder_RegisterInstanceTest()
        {
            const int expectedValue = 2;

            var testInstance = new TestClass { Value = expectedValue };

            var builder = new UnityContainerBuilder();

            builder.RegisterInstance(typeof(TestClass), (object)testInstance);

            Assert.That(builder.ServiceLocator.GetInstance<TestClass>().Value, Is.EqualTo(expectedValue));
        }

        [Test]
        public void CommonContainerBuilder_RegisterTypeTest()
        {
            var builder = new UnityContainerBuilder();

            builder.RegisterType<TestClass>();

            var serviceLocator = builder.ServiceLocator;

            var testInstance = serviceLocator.GetInstance<TestClass>();

            Assert.That(testInstance.Value, Is.EqualTo(DefaultTestValue));

            // make sure that the singleton status is operating
            testInstance.Value = 10;

            Assert.That(serviceLocator.GetInstance<TestClass>().Value, Is.EqualTo(10));
        }

        /** This behavior is inconsistent in the unity implementation.  I have posted this to the CommonServiceLocator
         * Unity implementation page to look for resolution.
         * 
         * [Test]
        public void CommonContainerBuilder_VerifyUnregisteredTypeBehaviorTest()
        {
            var builder = new UnityContainerBuilder();

            var serviceLocator = builder.ServiceLocator;

            Assert.Throws<ActivationException>(() => serviceLocator.GetInstance<TestClass>());
        }**/


        #region Test Class Implementation

        public class TestClass
        {
            public TestClass()
            {
                Value = DefaultTestValue;
            }

            public int Value { get; set; }
        }

        #endregion

    }
}
